package com.grwx.omc.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.grwx.omc.admin.entity.SysLookupType;
import com.grwx.omc.admin.entity.SysOrg;
import com.grwx.omc.admin.service.ISysLookupTypeService;
import com.grwx.omc.admin.service.ISysOrgService;
import com.grwx.omc.constant.Constant;
import com.grwx.omc.entity.ResponseData;
import com.grwx.omc.exception.ValidationException;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


@RestController
@RequestMapping("sys_org")
public class SysOrgController {

    @Autowired
    private ISysOrgService iSysOrgService;


    @ApiOperation(value = "组织机构管理.分页查询")
    @PostMapping("/search/{page}/{size}")
    public ResponseData page(@RequestBody(required = false) SysOrg sysOrg, @PathVariable("page") int page,
                             @PathVariable("size")  int size) {
        Page<SysOrg> sysOrgPage = new Page<>(page, size);
        IPage<SysOrg> result = iSysOrgService.page(sysOrgPage, sysOrg);
        return ResponseData.success(result);
    }


    @ApiOperation(value = "组织机构管理.新增组织机构")
    @PostMapping()
    public ResponseData insert(@RequestBody @Valid SysOrg sysOrg) {
        if (null == sysOrg) {
            throw new ValidationException(Constant.MsgCode.EMPTY_PARAM);
        }
        iSysOrgService.addOrUpdate(sysOrg);
        return ResponseData.success(Constant.MsgCode.INSERT_SUCCESS);
    }

    @ApiOperation(value = "组织机构管理.修改组织机构")
    @PutMapping()
    public ResponseData update(@RequestBody @Valid SysOrg sysOrg) {
        if (null == sysOrg) {
            throw new ValidationException(Constant.MsgCode.EMPTY_PARAM);
        }
        iSysOrgService.addOrUpdate(sysOrg);
        return ResponseData.success(Constant.MsgCode.UPDATE_SUCCESS);
    }

    @ApiOperation(value = "组织机构管理.删除组织机构")
    @DeleteMapping("/{id}")
    public ResponseData del(@PathVariable("id") Long id) {
        if (ObjectUtils.isEmpty(id)) {
            throw new ValidationException(Constant.MsgCode.EMPTY_DELETE);
        }
        iSysOrgService.removeById(id);
        return ResponseData.success(Constant.MsgCode.DELETE_SUCCESS);
    }

    @ApiOperation(value = "组织机构管理.批量删除组织机构")
    @DeleteMapping()
    public ResponseData batchDel(Long[] ids) {
        if (ObjectUtils.isEmpty(ids)) {
            throw new ValidationException(Constant.MsgCode.EMPTY_DELETE);
        }

        iSysOrgService.del(ids);
        return ResponseData.success(Constant.MsgCode.DELETE_SUCCESS);
    }
}
